package com.wangxiang.shop.product.service.impl;

import com.wangxiang.shop.product.dao.BaseTwoRelDao;
import com.wangxiang.shop.product.dao.ShopBaseDao;
import com.wangxiang.shop.product.entity.HttpAddEntity;
import com.wangxiang.shop.product.entity.ShopBaseEntity;
import com.wangxiang.shop.product.entity.ShopThreeTypeEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wangxiang.common.utils.PageUtils;
import com.wangxiang.common.utils.Query;

import com.wangxiang.shop.product.dao.ShopTwoTypeDao;
import com.wangxiang.shop.product.entity.ShopTwoTypeEntity;
import com.wangxiang.shop.product.service.ShopTwoTypeService;


@Service("shopTwoTypeService")
public class ShopTwoTypeServiceImpl extends ServiceImpl<ShopTwoTypeDao, ShopTwoTypeEntity> implements ShopTwoTypeService {
    @Autowired
    private ShopTwoTypeDao shopTwoTypeDao;
    @Autowired
    private BaseTwoRelDao baseTwoRelDao;
    @Autowired
    private ShopBaseDao shopBaseDao;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ShopTwoTypeEntity> page = this.page(
                new Query<ShopTwoTypeEntity>().getPage(params),
                new QueryWrapper<ShopTwoTypeEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public boolean add(HttpAddEntity addEntity) {
        //插入实体表
        ShopTwoTypeEntity shopTwoTypeEntity = new ShopTwoTypeEntity();
        shopTwoTypeEntity.setName(addEntity.getName());
        int i = shopTwoTypeDao.insertGetKey(shopTwoTypeEntity);
        if (i>0){
//            插入关系表
            int i1 = baseTwoRelDao.addAssociation(addEntity.getParentId(), shopTwoTypeEntity.getId());
            if (i1>0){
                return true;
            }
        }


        return false;
    }

    @Override
    public boolean getEntity(List<Integer> list ,String name) {
        if (list.size()==0){
            //该级第一个商品
            return true;
        }

        HashMap<String, Object> map = new HashMap<>();
        for (int i = 0; i < list.size(); i++) {
            ShopTwoTypeEntity shopTwoTypeEntity = shopTwoTypeDao.selectById(list.get(i));
            if (shopTwoTypeEntity.getName()==name|name.equals(shopTwoTypeEntity.getName())){
                map.put("name",shopTwoTypeEntity.getName());
            }
        }

        if (map.get("name") == null){
            return true;
        }

        return false;
    }

}